package io.geekidea.boot.shop.service.base.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.geekidea.boot.framework.exception.BusinessException;
import io.geekidea.boot.framework.page.OrderByItem;
import io.geekidea.boot.framework.page.OrderMapping;
import io.geekidea.boot.framework.page.Paging;
import io.geekidea.boot.shop.dto.FooterDto;
import io.geekidea.boot.shop.entity.Footer;
import io.geekidea.boot.shop.mapper.FooterMapper;
import io.geekidea.boot.shop.query.app.AppFooterQuery;
import io.geekidea.boot.shop.query.web.FooterQuery;
import io.geekidea.boot.shop.service.base.api.FooterService;
import io.geekidea.boot.shop.vo.app.AppFooterVo;
import io.geekidea.boot.shop.vo.web.FooterVo;
import io.geekidea.boot.util.PagingUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *  服务实现类
 *
 * @author geekidea
 * @since 2024-10-27
 */
@Slf4j
@Service
public class FooterServiceImpl extends ServiceImpl<FooterMapper, Footer> implements FooterService {

    @Autowired
    private FooterMapper footerMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addFooter(FooterDto dto) {
        Footer footer = new Footer();
        BeanUtils.copyProperties(dto, footer);
        return save(footer);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateFooter(FooterDto dto) {
        Integer id = dto.getId();
        Footer footer = getById(id);
        if (footer == null) {
            throw new BusinessException("不存在");
        }
        BeanUtils.copyProperties(dto, footer);
        return updateById(footer);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteFooter(Integer id) {
        return removeById(id);
    }

    @Override
    public FooterVo getFooterById(Integer id) {
        return footerMapper.getFooterById(id);
    }

    @Override
    public Paging<FooterVo> getFooterPage(FooterQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<FooterVo> list = footerMapper.getFooterPage(query);
        Paging<FooterVo> paging = new Paging<>(list);
        return paging;
    }

    @Override
    public AppFooterVo getAppFooterById(Integer id) {
        return footerMapper.getAppFooterById(id);
    }

    @Override
    public Paging<AppFooterVo> getAppFooterPage(AppFooterQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<AppFooterVo> list = footerMapper.getAppFooterPage(query);
        Paging<AppFooterVo> paging = new Paging<>(list);
        return paging;
    }

}
